﻿Option Infer On

Imports System.ComponentModel

Namespace Objects

    Public Class GrafiekData

        Private _datatable As HistoriekDataSet.HistoriekDataTable
        Private _values As List(Of String)
        Private _colors As List(Of Color)
        Private _horizontalSpan As Integer
        Private _startDatum As Date
        Private _eindDatum As Date
        Private _startwaarde As Integer ' = inventariswaarde bij begin vd grafiek, alleen gebruikt bij lijngrafiek

        Private _maxWaarde As Decimal
        Private _minWaarde As Decimal

        Public ReadOnly Property MaxWaarde() As Decimal
            Get
                Return _maxWaarde
            End Get
        End Property

        Public ReadOnly Property MinWaarde() As Decimal
            Get
                Return _minWaarde
            End Get
        End Property

        Public ReadOnly Property DataTable() As HistoriekDataSet.HistoriekDataTable
            Get
                Return _datatable
            End Get
        End Property

        Public ReadOnly Property HorizontalSpan() As Integer
            Get
                Return _horizontalSpan
            End Get
        End Property

        Public ReadOnly Property StartDatum() As Date
            Get
                Return _startDatum
            End Get
        End Property

        Public ReadOnly Property EindDatum() As Date
            Get
                Return _eindDatum
            End Get
        End Property

        Public ReadOnly Property Values() As List(Of String)
            Get
                Return _values
            End Get
        End Property

        Public Sub New(ByVal ds As HistoriekDataSet, ByVal type As GrafiekAreaBase.GrafiekType, ByVal startwaarde As Integer)
            _values = New List(Of String)
            _colors = New List(Of Color)
            _startwaarde = startwaarde

            If ds.Historiek.Any Then
                _startDatum = Utility.FirstDayOfMonth((From h In ds.Historiek Select h.Datum).Min)
                _eindDatum = Utility.FirstDayOfMonth((From h In ds.Historiek Select h.Datum).Max.AddMonths(1))
            Else
                _startDatum = New Date(Today.Year, 1, 1)
                _eindDatum = New Date(Today.Year, 12, 31)
            End If

            _horizontalSpan = EindDatum.Subtract(_startDatum).Days
            Select Case type
                Case GrafiekAreaBase.GrafiekType.Pin
                    CreateDataPerInstantie(ds)
                Case GrafiekAreaBase.GrafiekType.Blok
                    CreateDataPerMaand(ds)
                Case GrafiekAreaBase.GrafiekType.Lijn
                    CreateDataCumulatief(ds)
            End Select
            For i As Integer = 0 To _values.Count - 1
                _colors.Add(ColorManager.GetColor(i, 3))
            Next
        End Sub

        Public Function GetColor(ByVal naam As String) As Color
            Return _colors(_values.IndexOf(naam))
        End Function

        Private Sub CreateDataPerInstantie(ByVal dataset As HistoriekDataSet)
            _datatable = dataset.Historiek
            If Not dataset.Historiek.Any Then
                Return
            End If
            _maxWaarde = (From h In dataset.Historiek Select h.Waarde).Max
            _minWaarde = (From h In dataset.Historiek Select h.Waarde).Min
            _values = (From h In dataset.Historiek _
                       Select h.Naam _
                       Distinct).ToList()
        End Sub

        Private Sub CreateDataCumulatief(ByVal dataset As HistoriekDataSet)
            If Not dataset.Historiek.Any Then
                Return
            End If
            Dim ds As New HistoriekDataSet
            Dim cumulatieveWaarde As Integer = _startwaarde
            For Each item In (From h In dataset.Historiek _
                              Order By h.Datum)
                Dim row As HistoriekDataSet.HistoriekRow = ds.Historiek.NewHistoriekRow()
                row.Datum = item.Datum
                row.Naam = "Voorraad"
                cumulatieveWaarde += item.Waarde
                row.Waarde = cumulatieveWaarde
                ds.Historiek.AddHistoriekRow(row)
            Next
            _datatable = ds.Historiek
            _maxWaarde = (From h In ds.Historiek Select h.Waarde).Max
            _minWaarde = (From h In ds.Historiek Select h.Waarde).Min
            _values = (New String() {"Voorraad"}).ToList()
        End Sub

        Private Sub CreateDataPerMaand(ByVal dataset As HistoriekDataSet)
            Dim ds As New HistoriekDataSet
            For Each item In (From h In dataset.Historiek _
                      Group h By maand = Utility.FirstDayOfMonth(h.Datum), naam = h.Naam Into Group _
                      Select New With {.Datum = maand, _
                                       .Naam = naam, _
                                       .TotaleWaarde = Group.Sum(Function(p) p.Waarde)})
                Dim row As HistoriekDataSet.HistoriekRow = ds.Historiek.NewHistoriekRow()
                row.Datum = item.Datum
                row.Naam = item.Naam
                row.Waarde = item.TotaleWaarde
                ds.Historiek.AddHistoriekRow(row)
                If (Not _values.Contains(item.Naam)) Then _values.Add(item.Naam)
            Next
            _datatable = ds.Historiek
            If _datatable.Count = 0 Then
                Return
            End If
            _maxWaarde = (From h In _datatable _
                        Group h By maand = Utility.FirstDayOfMonth(h.Datum), naam = h.Naam Into Group _
                        Select Group.Sum(Function(p) p.Waarde)) _
                        .Max
            _minWaarde = (From h In _datatable _
                      Group h By maand = Utility.FirstDayOfMonth(h.Datum), naam = h.Naam Into Group _
                      Select Group.Sum(Function(p) p.Waarde)) _
                      .Min
        End Sub

    End Class

End Namespace
